﻿using AIStudio.Wpf.ECharts.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows.Input;

namespace AIStudio.Wpf.ECharts.ViewModels
{
    class MainWindowViewModel : BindableBase
    {
        public LineViewModel LineViewModel { get; set; } = new LineViewModel();

        public PieViewModel PieViewModel { get; set; } = new PieViewModel();

        public GaugeViewModel GaugeViewModel { get; set; } = new GaugeViewModel();

        public CandlestickViewModel CandlestickViewModel { get; set; } = new CandlestickViewModel();
    }

    class LineViewModel : BindableBase
    {
        private LabelClass _labels;
        public LabelClass Labels
        {
            get { return _labels; }
            set
            {
                if (_labels != value)
                {
                    SetProperty(ref _labels, value);
                }
            }
        }

        private ObservableCollection<ValuesClass> _valuesCollection;
        public ObservableCollection<ValuesClass> ValuesCollection
        {
            get { return _valuesCollection; }
            set
            {
                if (_valuesCollection != value)
                {
                    SetProperty(ref _valuesCollection, value);
                }
            }
        }

        private int _index;
        public int Index
        {
            get { return _index; }
            set
            {
                if (_index != value)
                {
                    SetProperty(ref _index, value);
                }
            }
        }

        private double _value;
        public double Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    SetProperty(ref _value, value);
                }
            }
        }

        private string _label;
        public string Label
        {
            get { return _label; }
            set
            {
                if (_label != value)
                {
                    SetProperty(ref _label, value);
                }
            }
        }

        private ICommand _addAxisCommand;
        public ICommand AddAxisCommand
        {
            get
            {
                return this._addAxisCommand ?? (this._addAxisCommand = new DelegateCommand(() => this.AddAxis()));
            }
        }

        private ICommand _addDataCommand;
        public ICommand AddDataCommand
        {
            get
            {
                return this._addDataCommand ?? (this._addDataCommand = new DelegateCommand(() => this.AddData()));
            }
        }

        private ICommand _deleteDataCommand;
        public ICommand DeleteDataCommand
        {
            get
            {
                return this._deleteDataCommand ?? (this._deleteDataCommand = new DelegateCommand(() => this.DeleteData()));
            }
        }


        private ICommand _addLabelCommand;
        public ICommand AddLabelCommand
        {
            get
            {
                return this._addLabelCommand ?? (this._addLabelCommand = new DelegateCommand(() => this.AddLabel()));
            }
        }

        private ICommand _deleteLabelCommand;
        public ICommand DeleteLabelCommand
        {
            get
            {
                return this._deleteLabelCommand ?? (this._deleteLabelCommand = new DelegateCommand(() => this.DeleteLabel()));
            }
        }

        public LineViewModel()
        {
            Labels = new LabelClass() { Labels = new ObservableCollection<string>() { "Jan", "Feb", "Mar", "Apr", "May", } };
            ValuesCollection = new ObservableCollection<ValuesClass>()
            {
                new ValuesClass(){ Values = new ObservableCollection<object>{ 4, 6, 5, 2 ,7 } },
                new ValuesClass(){ Values = new ObservableCollection<object>{ 6, 7, 3, 4 ,6 },Type ="bar" },
            };
        }

        private void AddAxis()
        {
            ValuesCollection.Add(new ValuesClass() { Values = new ObservableCollection<object> { 2, 8, 5, 9, 7 } });
        }

        private void AddData()
        {
            ValuesCollection[Index].Values.Add(Value);
        }

        private void DeleteData()
        {
            ValuesCollection[Index].Values.RemoveAt(0);
        }

        private void AddLabel()
        {
            Labels.Labels.Add(Label);
        }

        private void DeleteLabel()
        {
            Labels.Labels.RemoveAt(0);
        }
    }

    class PieViewModel : BindableBase
    {

        private ObservableCollection<PieValueClass> _valuesCollection;
        public ObservableCollection<PieValueClass> ValuesCollection
        {
            get { return _valuesCollection; }
            set
            {
                if (_valuesCollection != value)
                {
                    SetProperty(ref _valuesCollection, value);
                }
            }
        }

        private double _value;
        public double Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    SetProperty(ref _value, value);
                }
            }
        }

        private string _label;
        public string Label
        {
            get { return _label; }
            set
            {
                if (_label != value)
                {
                    SetProperty(ref _label, value);
                }
            }
        }

        private ICommand _addDataCommand;
        public ICommand AddDataCommand
        {
            get
            {
                return this._addDataCommand ?? (this._addDataCommand = new DelegateCommand(() => this.AddData()));
            }
        }

        private ICommand _deleteDataCommand;
        public ICommand DeleteDataCommand
        {
            get
            {
                return this._deleteDataCommand ?? (this._deleteDataCommand = new DelegateCommand(() => this.DeleteData()));
            }
        }


        public PieViewModel()
        {
            ValuesCollection = new ObservableCollection<PieValueClass>()
            {
                new PieValueClass(){ Value = 1,Label="Label1" },
                new PieValueClass(){ Value = 2,Label="Label2" },
            };
        }

        private void AddData()
        {
            ValuesCollection.Add(new PieValueClass() { Value = Value, Label = Label });
        }

        private void DeleteData()
        {
            ValuesCollection.RemoveAt(0);
        }

    }

    class GaugeViewModel : BindableBase
    {

        private GangeValueClass _data;
        public GangeValueClass Data
        {
            get { return _data; }
            set
            {
                if (_data != value)
                {
                    SetProperty(ref _data, value);
                }
            }
        }

        private double _value;
        public double Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    SetProperty(ref _value, value);
                }
            }
        }

        private string _label;
        public string Label
        {
            get { return _label; }
            set
            {
                if (_label != value)
                {
                    SetProperty(ref _label, value);
                }
            }
        }

        private ICommand _addDataCommand;
        public ICommand AddDataCommand
        {
            get
            {
                return this._addDataCommand ?? (this._addDataCommand = new DelegateCommand(() => this.AddData()));
            }
        }

        public GaugeViewModel()
        {
            Data = new GangeValueClass();
        }

        private void AddData()
        {
            Data.Value = Value;
            Data.Label = Label;
        }

    }

    class CandlestickViewModel : BindableBase
    {

        private ObservableCollection<List<object>> _valuesCollection;
        public ObservableCollection<List<object>> ValuesCollection
        {
            get { return _valuesCollection; }
            set
            {
                if (_valuesCollection != value)
                {
                    SetProperty(ref _valuesCollection, value);
                }
            }
        }

        public CandlestickViewModel()
        {
            using (Stream resourceStream = new FileStream("SH600747.TXT", FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(resourceStream, Encoding.GetEncoding("GB2312")))
                {
                    //读每一行
                    var strings = reader.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    //获取股票名称
                    string stockName = strings[0].Replace("\r", "");

                    var data = new ObservableCollection<List<object>>();

                    //第一行是股票名称, 第二行是类型名称, 第3行才是股票数据
                    for (int i = 2; i < strings.Length; i++)
                    {
                        string line = strings[i];
                        string[] subLines = line.Split('\t');

                        DateTime date = DateTime.Parse(subLines[0]);
                        Double open = Double.Parse(subLines[1]);
                        Double high = Double.Parse(subLines[2]);
                        Double low = Double.Parse(subLines[3]);
                        Double close = Double.Parse(subLines[4]);
                        Double volumn = Double.Parse(subLines[5]);

                        data.Add(
                            new List<object>
                            {
                                date.ToString("yyyy-MM-dd"),
                                open,
                                high,
                                low,
                                close,
                                volumn
                            });
                    }

                    ValuesCollection = data;
                }
            }
        }

        private ICommand _addDataCommand;
        public ICommand AddDataCommand
        {
            get
            {
                return this._addDataCommand ?? (this._addDataCommand = new DelegateCommand(() => this.AddData()));
            }
        }

        private void AddData()
        {
            using (StreamReader r = new StreamReader("stock-DJI.json"))
            {

                string json = r.ReadToEnd();

                ValuesCollection = JsonConvert.DeserializeObject<ObservableCollection<List<object>>>(json);

            }
        }
    }
}
